package com.hank.activemq;

import org.apache.activemq.broker.BrokerFactory;
import org.apache.activemq.broker.BrokerService;
import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
import org.springframework.context.ApplicationContext;

import java.net.URI;

public class BrokerMainRun {

    public static void main(String[] args) throws Exception {
//        startForJava();

//        startForFactory();

        startForSpring();
        Thread th=new Thread(){
            public void run(){
                while (true){
                    try {
                        Thread.sleep(10000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };th.start();
    }

    public  static void startForSpring(){
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        System.out.println("Broker is Startd success!");
    }

    public static void startForJava() throws Exception {
        BrokerService brokerService=new BrokerService();
        brokerService.setBrokerName("ITPub");
        brokerService.setUseJmx(true); //开启监控
        brokerService.setPersistent(true);//持久化
        brokerService.addConnector("tcp://127.0.0.1:61616");
        brokerService.start();
        System.out.println("Broker is Startd success!");

    }

    public static void startForFactory() throws Exception {
        String uri = "properties:activemq.properties";
        BrokerService brokerService =BrokerFactory.createBroker(new URI(uri));
        brokerService.addConnector("tcp://127.0.0.1:61616");
        brokerService.start();
        System.out.println("Broker 启动成功!");
    }

}
